<template>
  <div class="login-container">
    <Owl :close-eyes="isFocus" />
    <div class="login-card">
      <div class="title">
        <img src="@/assets/images/layout/logo-text-2.png">
      </div>
      <div class="content">
        <el-tabs
          type="border-card"
          class="login-tabs"
          v-model="activeTab"
        >
          <el-tab-pane name="account">
            <template #label>
              <span class="login-tabs-label">
                <el-icon>
                  <User />
                </el-icon>
                <span>账号登录</span>
              </span>
            </template>
            <Account
              ref="accountRef"
              v-bind="payload"
            />
          </el-tab-pane>
          <el-tab-pane name="phone">
            <template #label>
              <span class="login-tabs-label">
                <el-icon>
                  <Iphone />
                </el-icon>手机登录
              </span>
            </template>
            <Phone v-bind="payload" />
          </el-tab-pane>
        </el-tabs>

        <div class="control">
          <el-checkbox
            v-model="isRemember"
            label="记住密码"
            size="large"
          />
          <el-link type="primary">忘记密码</el-link>
        </div>
        <el-button
          :loading="loading"
          type="primary"
          size="large"
          @click.prevent="handleLogin"
        >
          登 录
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="Login">
import Owl from './component/Owl.vue'
import Account from './component/Account.vue'
import Phone from './component/Phone.vue'
import { useFocus } from './useFocus'
import { sessionCache } from '@/utils/cache/cookies'
import { CacheKey } from '@/utils/constants/cache-key'
const { isFocus, handleBlur, handleFocus } = useFocus()

const isRemember = ref<boolean>(
  sessionCache.getCache(CacheKey.ISREMEMBER) ?? false
)
watch(isRemember, (value) => {
  value
    ? sessionCache.setCache(CacheKey.ISREMEMBER, value)
    : sessionCache.removeCache(CacheKey.ISREMEMBER)
})

/** Account组件的引用 */
const activeTab = ref('account')
const accountRef = ref<InstanceType<typeof Account>>()
/** 登录按钮 Loading */
const loading = ref(false)
function updateLoading(isload: boolean) {
  loading.value = isload
}
const payload = {
  updateLoading,
  handleBlur,
  handleFocus
}
/** 登录 */
function handleLogin() {
  if (activeTab.value === 'account') {
    accountRef.value?.handleLogin(isRemember.value)
  } else {
    console.log('手机号登录-----开发中')
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;
  background-image: url('../../assets/images/background.svg');
  background-repeat: no-repeat;
  background-position: center 110px;
  background-size: 100%;
  .login-card {
    width: 480px;
    max-width: 90%;
    border-radius: 20px;
    box-shadow: 0 0 10px #dcdfe6;
    background-color: var(--el-bg-color);
    overflow: hidden;
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 150px;
      img {
        height: 100%;
      }
    }
    .content {
      padding: 20px 50px 50px 50px;
      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 40px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }
      .control {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .el-button {
        width: 100%;
        margin-top: 10px;
      }
    }
  }
}
:deep(.login-tabs) {
  .el-tabs__nav {
    width: 100%;
  }
  .el-tabs__item {
    flex-grow: 1;
  }
  .login-tabs-label {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
